package cmd

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/cobra"
	"smmf/config"
	"smmf/control"
	"smmf/log"
	"smmf/model"
)

var rootCmd = &cobra.Command{
	Use:   "start",
	Short: "食面埋福小程序启动了",
	Long:  `食面埋福小程序点餐系统`,
	Run: func(cmd *cobra.Command, args []string) {
		config.InitConfig()
		err := model.InitDb(config.MysqlDsn)
		if err != nil {
			log.Error(err.Error())
		}
		err = model.InitRedis()
		if err != nil {
			log.Error(err.Error())
		}
		r := gin.Default()
		r.Use(control.Cors())
		gin.SetMode("debug")
		r.Static("doc", config.GetRootPath()+"/doc")
		r.Static("upload", config.GetRootPath()+"/upload")
		control.SetRouter(r)
		r.Run(config.Address)
		return
	},
}

func Execute() error {
	return rootCmd.Execute()
}
